//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System.Collections.ObjectModel;
    using System.Windows;
    using ManagementStudioServices.Interfaces;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using Microsoft.Research.ScientificWorkflow.UIDesigner.WpfComposite;
    using Microsoft.Practices.Unity;
    using Microsoft.Research.DataLayer;

    public class SelectGroupPresenter
    {

        private SelectGroup selectGroupWindow;

        /// <summary>
        /// Add User command.
        /// </summary>
        public DelegateCommand<object> OkCommand { get; set; }

        /// <summary>
        /// Cancel User command.
        /// </summary>
        public DelegateCommand<object> CancelCommand { get; set; }


        public bool HasNoGroup
        {
            get
            {
                return !HasGroup;
            }
        }

        public bool HasGroup
        {
            get
            {
                return this.GroupList.Count > 0;
            }
        }

        /// <summary>
        /// Show Add User command.
        /// </summary>
        public DelegateCommand<object> ShowCommand { get; set; }


        public ObservableCollection<SelectGroupModel> GroupList { get; set; }

        public ObservableCollection<SearchUserModel> SearchResults { get; set; }


        public SelectGroupModel SelectGroupModel { get; set; }

        private IUnityContainer container;

        private IRegistryManagerService registryManagerService;

        public string DeleteGroupName { get; set; }

        public SelectGroupPresenter(string groupName, IUnityContainer container, IRegistryManagerService service)
        {
            this.container = container;
            this.registryManagerService = service;
            this.DeleteGroupName = groupName;
          
            this.OkCommand = new DelegateCommand<object>(p => this.OkCommandExecuted());
            this.ShowCommand = new DelegateCommand<object>(p => this.ShowCommandExecuted());
            this.CancelCommand = new DelegateCommand<object>(p => this.CancelCommandExecuted());
            this.GenerateUserModel();
        }

        private void GenerateUserModel()
        {
            this.GroupList = new ObservableCollection<SelectGroupModel>();
            Collection<Group> groupCollection = this.registryManagerService.GetAllGroupByFilter(this.DeleteGroupName);
            foreach (Group group in groupCollection)
            {
                SelectGroupModel selectModel = new SelectGroupModel(group.Name, ((IObject)group).ID);
                this.GroupList.Add(selectModel);
            }
        }

        /// <summary>
        /// Handler for Adding New User
        /// </summary>
        private void OkCommandExecuted()
        {
            if (this.SelectGroupModel != null)
            {
                this.CloseGroupWindow();
            }
        }
     
        /// <summary>
        /// Handler for Showing "Add User" Screen
        /// </summary>
        private void ShowCommandExecuted()
        {
            this.selectGroupWindow = new SelectGroup();
            this.selectGroupWindow.Owner = (Window)this.container.Resolve<IRegionManager>("MainWindow");
            this.selectGroupWindow.DataContext = this;
            this.selectGroupWindow.ShowDialog();
        }

        /// <summary>
        /// Handler for Canceling Select Group window
        /// </summary>
        private void CancelCommandExecuted()
        {
            this.CloseGroupWindow();
            this.SelectGroupModel = null;
        }

        /// <summary>
        /// Closes the Group select window
        /// </summary>
        private void CloseGroupWindow()
        {
            if (this.selectGroupWindow != null)
            {
                this.selectGroupWindow.Close();
                this.selectGroupWindow = null;
            }
        }

    }
}
